package com.example.music.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dao.SongMapper;
import com.example.music.dto.ConsumerSexDto;
import com.example.music.entity.Consumer;
import com.example.music.entity.Singer;
import com.example.music.entity.Song;
import com.example.music.service.SongService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ContentValue;
import com.example.music.utils.FileMd5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SongServiceImpl extends BaseServiceImpl<Song,SongMapper> implements SongService {
    @Autowired
    SongMapper songMapper;

    @Override
    protected SongMapper getMapper() {
        return songMapper;
    }

    @Override
    public AjaxJson addSong(Song song, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"song";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        //实际文件地址
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = ContentValue.Slash+"song"+ContentValue.Slash+fileName;
        try {
            file.transferTo(dest);

            String fileMd5 = FileMd5.getFileMd5(dest);
            List<Song> songs = songMapper.selectList(null);
            for (Song songx : songs) {
                if (songx.getMd5().equals(fileMd5)) {
                    song.setLyric(songx.getLyric());
                    song.setMd5(songx.getMd5());
                    song.setUrl(songx.getUrl());
                    song.setPic(songx.getPic());
                    Integer add = add(song);
                    if (add == 1){
                        return AjaxJson.getSuccess();
                    }
                    return AjaxJson.getError();
                }
            }

            song.setUrl(mysqlDir);
            song.setPic("/img/songPic/tubiao.jpg");


            Integer add = add(song);
            if (add == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson updateSingerPic(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"img"+
                ContentValue.Slash+"songPic";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = ContentValue.Slash+"img"+ContentValue.Slash+"songPic"+ContentValue.Slash+fileName;
//        String mysqlDir = "/img/songPic/"+fileName;
        try {
            file.transferTo(dest);
            Song song = new Song();
            song.setId(id.longValue());
            song.setPic(mysqlDir);
            Integer update = songMapper.updateById(song);
            if (update == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson updateSingerUrl(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"song";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = ContentValue.Slash+"song"+ContentValue.Slash+fileName;
        try {
            file.transferTo(dest);

            String fileMd5 = FileMd5.getFileMd5(dest);
            List<Song> songs = songMapper.selectList(null);
            for (Song song : songs) {
                if (song.getMd5().equals(fileMd5)) {
                    Song song1 = new Song();

                    song1.setId(id.longValue());
                    song1.setUrl(song.getUrl());
                    song1.setMd5(song.getMd5());

                    UpdateWrapper<Song> songUpdateWrapper = new UpdateWrapper<>();
                    songUpdateWrapper.eq("id", song1.getId());
                    songUpdateWrapper.set("md5",song.getMd5());
                    songUpdateWrapper.set("url",song.getUrl());
                    songUpdateWrapper.set("lyric",song.getLyric());
                    Integer update = songMapper.update(song1,songUpdateWrapper);
                    if (update == 1){
                        dest.delete();
                        return AjaxJson.getSuccess();
                    }
                    return AjaxJson.getError();
                }
            }
            Song song = new Song();
            song.setId(id.longValue());
            song.setMd5(fileMd5);
            song.setUrl(mysqlDir);
            Integer update = songMapper.updateById(song);
            if (update == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson songOfSongName(String songName) {
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Song> name = songQueryWrapper.like("name", songName);
        List<Song> songs = songMapper.selectList(name);
        return AjaxJson.getSuccessData(songs);
    }

    @Override
    public AjaxJson songOfSingerName(String singerName) {
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Song> name = songQueryWrapper.like("name", singerName);
        List<Song> songs = songMapper.selectList(name);
        return AjaxJson.getSuccessData(songs);
    }
}
